function registerEvent() {
    registerAddBtnChange();
}

function registerAddBtnChange() {

    $("#add").after($("#addbtn"));
    $("#add").menubutton();
    $("#addbtn").menu();
}

function viewAnd(name) {
    var expression = document.getElementById("expression/" + name).value;
    if (expression == "between") {
        document.getElementById("and/" + name).hidden = false;
        document.getElementById("and/" + name).style.display="";
        document.getElementById("max/" + name).hidden = false;
        document.getElementById("max/" + name).style.display="";
    } else {
        document.getElementById("and/" + name).hidden = true;
        document.getElementById("and/" + name).style.display="none";
        document.getElementById("max/" + name).hidden = true;
        document.getElementById("max/" + name).style.display="none";
        document.getElementById("max/" + name).value="";
    }
}

function initData(url, data) {
    document.getElementById(url).value = data;
    if (expression == "between") {
        document.getElementById("and/" + name).hidden = false;
        document.getElementById("and/" + name).style.display="";
        document.getElementById("max/" + name).hidden = false;
        document.getElementById("max/" + name).style.display="";
    } else {
        document.getElementById("and/" + name).hidden = true;
        document.getElementById("and/" + name).style.display="none";
        document.getElementById("max/" + name).hidden = true;
        document.getElementById("and/" + name).style.display="none";
         document.getElementById("max/" + name).value="";

    }
}

function loadData(url, data) {
    if(data!=null&&data!=""){
    document.getElementById(url).options[data].selected = true;}
}

function changeData(url, data) {
    if(document.getElementById("and/" + data)!=null){
    if (url == "between") {
        document.getElementById("and/" + data).hidden = false;
        document.getElementById("and/" + data).style.display="";
        document.getElementById("max/" + data).hidden = false;
        document.getElementById("max/" + data).style.display="";
    } else {
        document.getElementById("and/" + data).hidden = true;
        document.getElementById("and/" + data).style.display="none";
        document.getElementById("max/" + data).hidden = true;
        document.getElementById("and/" + data).style.display="none";
        document.getElementById("max/" + data).value="";
    }}
}

function hideEndpoint() {
        var obj = $("#endpoint_field");
        var icon = $("#endpoint_all");
        if($("#endpoint_all").is(":checked")) {
            $("#endpoint_field").find("input").each(function(){
                if($(this).is(":checked")) {
                    $(this).attr("checked",false);
                }
                $(this).attr("disabled","disabled");
            });
            $("#endpoint_field").find("select").each(function(){
                $(this).attr("disabled","disabled");
            });
        } else {
            $("#endpoint_field").find("input").each(function(){
                $(this).removeAttr("disabled");
                $(this).addClass("editordisable");
                $(this).removeClass("x-gui-texteditor");
            });
            $("#endpoint_field").find("select").each(function(){
                $(this).removeAttr("disabled");
            });
        }
    }

function changePriority() {
    $("#priorityContent").html('&nbsp'+$("select[name='AssignRule/priority']").val()+'&nbsp');
}

function prepareSortAssignRules(){
    $("table.ui-list-table tbody").sortable({cursor: "move", items: "tr:not(.default)"});
    $("table.ui-list-table tbody tr").disableSelection();
    $("table.ui-list-table tbody").bind('sortstop', function(event, ui){
        draged = ui.item;
        div = draged.find("div.ui-checker")[0];
        dragedKey = div.getAttribute("value");
        next = ui.item.next();
        div = next.find("div.ui-checker")[0];
        nextKey = div.getAttribute("value");
        //直接发出PUT方法，现在服务器还没法支持
        $.ajax({type: "POST", url: "/assign_rules/move/" + dragedKey + "/" + nextKey + "?_method=PUT"}).done(function(){
            window.location.reload()
        })
    })
}